/**************************************************************

*This do file creates Appendix Figure 3, which uses the imputation estimator from Borusyak et al. 2021;

**************************************************************/
capture log close
capture program drop _all
capture macro drop _all
estimates drop _all
set matsize 2000
drop _all
set more off


*UPDATE PATH NAMES;
global ched_data 
global output 

***************************************************************
#delimit ;
use "${ched_data}analysis_sample.dta" ;

xtset provcode year;

*SET CONTROLS;
global controls_yr emp_f_2564_90_yr* emp_m_2564_90_yr* female_90_yr* age_90_yr* nurse_cen_pc90_yr*;
global controls_yr_full emp_f_2564_90_yr* emp_m_2564_90_yr* female_90_yr* age_90_yr* nurse_cen_pc90_yr* _yrschl_2564_90_yr* urban_90_yr*  ;
global controls emp_f_2564_90 emp_m_2564_90 female_90 age_90 nurse_cen_pc90;

*Passers out of grad pop;
gen pass_rate_grad=(passed/collegepop)*100;
replace pass_rate_grad=0 if examinees==.;


gen treatment=2000 if high1==1;
gen treatment4y=2003 if high1==1;

*****************************;
*DID Imputations;
*Use lincom to make 1999 omitted year;
#delimit ;
foreach var of varlist migrateUSA_hth enroll_tot1 pass_rate_grad nurseprog {;

	if "`var'"=="migrateUSA_hth" {; local v="ylabel(-2(2)11) ymtick(-2(1)11)"; };
	if "`var'"=="enroll_tot1" {; local v="ylabel(-3(2)7) ymtick(-3(1)7)"; };
	if "`var'"=="nurseprog" {; local v="ylabel(-1(2)8) ymtick(-1(1)8)"; };
	if "`var'"=="pass_rate_grad" {; local v="ylabel(0(.2)1.2) ymtick(0(.1)1.2)"; };
	
	if "`var'"=="migrateUSA_hth" {; local w="1991"; };
	if "`var'"=="enroll_tot1" {; local w="1991"; };
	if "`var'"=="nurseprog" {; local w="1991"; };
	if "`var'"=="pass_rate_grad" {; local w="1991"; };
	
	if "`var'"=="migrateUSA_hth" {; local p="9"; };
	if "`var'"=="enroll_tot1" {; local p="9"; };
	if "`var'"=="nurseprog" {; local p="6"; };
	if "`var'"=="pass_rate_grad" {; local p="9"; };
	
	if "`var'"=="migrateUSA_hth" {; local c="Nurse Migrants per 100000"; };
	if "`var'"=="enroll_tot1" {; local c="Percentage Points"; };
	if "`var'"=="nurseprog" {; local c="Nursing Programs"; };
	if "`var'"=="pass_rate_grad" {; local c="Nursing Exam Pass Rate (%)"; };
	
	did_imputation `var' provcode year treatment if year>=1991&year<=2013, timecontrols(emp_f_2564_90 emp_m_2564_90 female_90 age_90 nurse_cen_pc90) cluster(provcode) allhorizons pretrends(9	); 
		
	event_plot, savecoef;

	preserve;
	
	keep __event*;
	keep if __event_H1!=.;
	gen year=__event_pos1+2000;

	lincom pre9-pre1;
	gen _b_99base=r(estimate) if year==1991;
	gen _se_99base=r(se) if year==1991;

	lincom pre8-pre1;
	replace _b_99base=r(estimate) if year==1992;
	replace _se_99base=r(se) if year==1992;

	lincom pre7-pre1;
	replace _b_99base=r(estimate) if year==1993;
	replace _se_99base=r(se) if year==1993;

	lincom pre6-pre1;
	replace _b_99base=r(estimate) if year==1994;
	replace _se_99base=r(se) if year==1994;

	lincom pre5-pre1;
	replace _b_99base=r(estimate) if year==1995;
	replace _se_99base=r(se) if year==1995;

	lincom pre4-pre1;
	replace _b_99base=r(estimate) if year==1996;
	replace _se_99base=r(se) if year==1996;

	lincom pre3-pre1;
	replace _b_99base=r(estimate) if year==1997;
	replace _se_99base=r(se) if year==1997;

	lincom pre2-pre1;
	replace _b_99base=r(estimate) if year==1998;
	replace _se_99base=r(se) if year==1998;

	lincom pre1-pre1;
	replace _b_99base=r(estimate) if year==1999;
	replace _se_99base=r(se) if year==1999;

	forvalues x=0/9 {;
		lincom tau`x'-pre1;
		replace _b_99base=r(estimate) if year==200`x';
		replace _se_99base=r(se) if year==200`x';
	};

	forvalues x=10/13 {;
		lincom tau`x'-pre1;
		replace _b_99base=r(estimate) if year==20`x';
		replace _se_99base=r(se) if year==20`x';
	};

	replace _se_99base=. if year==1999;

	gen upper_=_b_99base+(_se_99base*1.96);
	gen lower_=_b_99base-(_se_99base*1.96);

	drop if year<1991;

	#delimit ;		
	twoway (line _b_99base year, lcolor(black) lpattern(solid))
		(line upper_  year, lcolor(black) lpattern(dot))
		(line lower_ year, lcolor(black) lpattern(dot)),
		legend(off)
		xtitle("Year") ytitle("Percentage Points")
		yline(0, lstyle(shortdash_dot)) ylab(, nogrid) ytitle(`c')
		plotregion(fcolor(white)) graphregion(fcolor(white))
		xline(2000 2007, lpattern(solid) lcolor(gs14))
		`v';
		
		graph export "${output}Main Figures/bjs_`var'.pdf", replace;
		
		restore;


};
foreach var of varlist grad_tot {;

	if "`var'"=="grad_tot" {; local v="ylabel(-2(1)3) ymtick(-2(1)3)"; };
	
	if "`var'"=="grad_tot" {; local w="1998"; };
	
	if "`var'"=="grad_tot" {; local p="2"; };
	
	if "`var'"=="grad_tot" {; local c="Percentage Points"; };
	
	did_imputation `var'  provcode year treatment if year>=`w'&year<=2013, timecontrols(emp_f_2564_90 emp_m_2564_90 female_90 age_90 nurse_cen_pc90) cluster(provcode) allhorizons pretrends(2	); 
		
	event_plot, savecoef;
	preserve;
	
	keep __event*;
	keep if __event_H1!=.;
	gen year=__event_pos1+2000;

	lincom pre2-pre1;
	gen _b_99base=r(estimate) if year==1998;
	gen _se_99base=r(se) if year==1998;

	lincom pre1-pre1;
	replace _b_99base=r(estimate) if year==1999;
	replace _se_99base=r(se) if year==1999;

	forvalues x=0/9 {;
		lincom tau`x'-pre1;
		replace _b_99base=r(estimate) if year==200`x';
		replace _se_99base=r(se) if year==200`x';
	};

	forvalues x=10/13 {;
		lincom tau`x'-pre1;
		replace _b_99base=r(estimate) if year==20`x';
		replace _se_99base=r(se) if year==20`x';
	};

	replace _se_99base=. if year==1999;

	gen upper_=_b_99base+(_se_99base*1.96);
	gen lower_=_b_99base-(_se_99base*1.96);

	drop if year<1991;

	#delimit ;		
	twoway (line _b_99base year, lcolor(black) lpattern(solid))
		(line upper_  year, lcolor(black) lpattern(dot))
		(line lower_ year, lcolor(black) lpattern(dot)),
		legend(off)
		xtitle("Year") ytitle("Percentage Points")
		yline(0, lstyle(shortdash_dot)) ylab(, nogrid) ytitle(`c')
		plotregion(fcolor(white)) graphregion(fcolor(white))
		xline(2000 2007, lpattern(solid) lcolor(gs14))
		`v';
		
		graph export "${output}Main Figures/bjs_`var'.pdf", replace;
		restore;


};

*NURSING GRAD;
#delimit ;
foreach var of varlist grad_tot1 {;

	if "`var'"=="grad_tot1" {; local v="ylabel(-1(0.5)1.5) ymtick(-1(.25)1.5)"; };
	
	if "`var'"=="grad_tot1" {; local w="1998"; };
	
	if "`var'"=="grad_tot1" {; local p="5"; };
	
	did_imputation `var'  provcode year treatment4y if year>=`w'&year<=2013, timecontrols(emp_f_2564_90 emp_m_2564_90 female_90 age_90 nurse_cen_pc90) cluster(provcode) allhorizons pretrends(5); 
		
	event_plot, savecoef;
	
	preserve;
	keep __event*;
	keep if __event_H1!=.;
	gen year=__event_pos1+2000;

	lincom pre5-pre1;
	gen _b_99base=r(estimate) if year==1998;
	gen _se_99base=r(se) if year==1998;

	lincom pre4-pre1;
	replace _b_99base=r(estimate) if year==1999;
	replace _se_99base=r(se) if year==1999;

	lincom pre3-pre1;
	replace _b_99base=r(estimate) if year==2000;
	replace _se_99base=r(se) if year==2000;

	lincom pre2-pre1;
	replace _b_99base=r(estimate) if year==2001;
	replace _se_99base=r(se) if year==2001;

	lincom pre1-pre1;
	replace _b_99base=r(estimate) if year==2002;
	replace _se_99base=r(se) if year==2002;

	forvalues x=0/9 {;
		lincom tau`x'-pre1;
		replace _b_99base=r(estimate) if year==200`x';
		replace _se_99base=r(se) if year==200`x';
	};

	forvalues x=10/10 {;
		lincom tau`x'-pre1;
		replace _b_99base=r(estimate) if year==20`x';
		replace _se_99base=r(se) if year==20`x';
	};

	replace _se_99base=. if year==1999;

	gen upper_=_b_99base+(_se_99base*1.96);
	gen lower_=_b_99base-(_se_99base*1.96);

	drop if year<1991;

	#delimit ;		
	twoway (line _b_99base year, lcolor(black) lpattern(solid))
		(line upper_  year, lcolor(black) lpattern(dot))
		(line lower_ year, lcolor(black) lpattern(dot)),
		legend(off)
		xtitle("Year") ytitle("Percentage Points")
		yline(0, lstyle(shortdash_dot)) ylab(, nogrid) ytitle(`c')
		plotregion(fcolor(white)) graphregion(fcolor(white))
		xline(2000 2007, lpattern(solid) lcolor(gs14))
		`v';
		
		graph export "${output}Main Figures/bjs_`var'.pdf", replace;
		
		restore;


};
